package com.wxzz.elearing.pay.api.admin;


import com.alibaba.fastjson.JSONObject;
import com.wxzz.elearing.pay.dto.UserBankCardDto;
import com.wxzz.elearing.pay.entity.UserBankCard;
import com.wxzz.elearing.pay.entity.UserTransaction;
import com.wxzz.elearing.pay.service.IUserBankCardService;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author
 * @since 2021-04-23
 */
@RestController
@RequestMapping("/user-bank-card")
@Api(tags = "银行卡接口(后台)")
public class UserBankCardController {

    @Autowired
    private IUserBankCardService userBankCardService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @ApiOperation(value = "添加或修改银行卡(后台)", notes = "添加或修改银行卡(后台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "saveUserBankCard", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<UserTransaction>>> saveUserBankCard(@RequestHeader String token, @RequestBody UserBankCard userBankCard) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return userBankCardService.saveUserBankCard(jsonObject, userBankCard);
    }

    @ApiOperation(value = "列出我的银行卡(后台)", notes = "列出我的银行卡(后台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "listUserBankCardByUser", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<UserBankCard>>> listUserBankCardByUser(@RequestHeader String token, @RequestBody UserBankCardDto userBankCardDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return userBankCardService.listUserBankCardByUser(jsonObject, userBankCardDto);
    }

    @ApiOperation(value = "删除银行卡(后台)", notes = "删除银行卡(后台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "deleteUserBankCardById/{userBankCardId}", method = RequestMethod.POST)
    public ReturnVO deleteUserBankCardById(@PathVariable Long userBankCardId) {
        return userBankCardService.deleteUserBankCardById(userBankCardId);
    }

    @ApiOperation(value = "修改银行卡信息时发送验证码(后台)", notes = "修改银行卡信息时发送验证码(后台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "sendUpdateBankCardCode", method = RequestMethod.POST)
    public ReturnVO sendUpdateBankCardCode(@RequestHeader String token) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return userBankCardService.sendUpdateBankCardCode(jsonObject.getString("phone"));
    }

    @ApiOperation(value = "校验验证码(后台)", notes = "校验验证码(后台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "verifyCode/{code}", method = RequestMethod.POST)
    public ReturnVO verifyCode(@RequestHeader String token, @PathVariable String code) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return userBankCardService.verifyCode(jsonObject.getString("phone"), code);
    }

}
